package com.osgix.authorize.dao.test;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.core.annotation.Order;

import com.osgix.authorize.dao.UserInfoDao;
import com.osgix.authorize.model.UserInfo;
import com.osgix.authorize.vo.query.UserInfoQuery;
import com.osgix.common.orm.ibatis.Page;
import com.osgix.common.test.AbsDaoTestCase;

public class DaoTestCase extends AbsDaoTestCase {
	private static final Logger logger = Logger.getLogger(DaoTestCase.class);

	@Resource
	UserInfoDao userInfoDao;

	// @Test
	@Order(1)
	public void saveUser() {
		UserInfo user = new UserInfo();
		// user.setUserId(10100L);
		user.setLoginAccount("hahaha");
		user.setLoginPwd("ppp");
		user.setFirstName("jjj");
		user.setUserName("nameuuu");
		user.setLoginEmail("eeemmmaaa");
		userInfoDao.save(user);
		System.out.println(">>>> userId=" + user.getUserInfoId());
	}

	//@Test
	@Order(1)
	public void saveUsers() {
		for (int i = 0; i < 100; i++) {
			UserInfo user = new UserInfo();
			//user.setUserInfoId((long) i);
			user.setLoginAccount("hahaha" + i);
			user.setLoginPwd("ppp");
			user.setFirstName("jjj");
			user.setUserName("nameuuu");
			user.setLoginEmail("eeemmmaaa" + i);
			userInfoDao.save(user);
			System.out.println(">>>> userId=" + user.getUserInfoId());
		}
	}

	//@Test
	@Order(2)
	public void getCount() {
		System.out.println(userInfoDao.getCount());
	}

	@Test
	@Order(2)
	public void getPage() {
		UserInfoQuery uiq = new UserInfoQuery();
		uiq.setLastName("a");
		uiq.setPageNumber(3);
		uiq.setPageSize(8);
		uiq.setSortColumns(uiq.FIRST_NAME + uiq.ORDER_DESC);
		Page<UserInfo> p = userInfoDao.findPage(uiq);
		List<UserInfo> ul = p.getResult();
		for (Iterator iterator = ul.iterator(); iterator.hasNext();) {
			UserInfo userInfo = (UserInfo) iterator.next();
			System.out.println(userInfo.getUserInfoId() + " " + userInfo.getLoginAccount());
		}
	}

	//@Test
	public void findAll() {
		UserInfoQuery uiq = new UserInfoQuery();
		List<UserInfo> ul = userInfoDao.findAll(new String[] { uiq.FIRST_NAME + uiq.ORDER_DESC });

		for (Iterator iterator = ul.iterator(); iterator.hasNext();) {
			UserInfo userInfo = (UserInfo) iterator.next();
			System.out.println(userInfo.getUserInfoId() + " " + userInfo.getLoginAccount());
		}
	}

	//@Test
	public void findUnique() {
		UserInfoQuery uiq = new UserInfoQuery();
		uiq.setLoginAccount("hahaha7");
		UserInfo user = userInfoDao.findUnique(uiq);
		System.out.println(user.getUserName());
	}

}
